/**
 * 两个业务类之间的关系抽象
 */
package indi.zjh.dp.orm;

import indi.zjh.dp.db.TField;
import indi.zjh.dp.db.Table;

public class RelationStrategy {
	public static final int EXTEND = 0;
	public static final int COMPOSITION = 1;
	public static final int AGGREGATION = 2;
	public static final int ASSOCIATION = 3;
	public static final int DEPENDENCY = 4;
	public static final int INIT = 8888;
	
	

	
//	public static String getInsertSql(RelationClass a, RelationClass b)
//	{
//		String tableName = BusinessMapDataStrategy.getRelationTableName(a, b);
//		Table t = new Table(tableName);
//		TField tf = new TField();
//		tf.name = "aid";
//		t.addField(tf);
//		
//		tf = new TField();
//		tf.name = "bid";
//		t.addField(tf);
//		
//		tf = new TField();
//		tf.name = "_t";
//		t.addField(tf);
//
//		return t.getInsertSql();
//	}
	
//	public static Object[] getInsertParam(long aid, long bid, int type)
//	{
//		return new Object[]{aid, bid, type};
//	}
//	
//	public static String getRelateValueSql(RelationClass a, RelationClass b, long aid, int type)
//	{
//		String tableName = BusinessMapDataStrategy.getRelationTableName(a, b);
//		String tableNameR = BusinessMapDataStrategy.getRelationTableName(b, a);
//		if(type == ASSOCIATION)
//		{
//			String tableNameB = BusinessMapDataStrategy.getTableName(b);
//			System.out.println("SELECT DISTINCT `"+tableName+"`.`bid` FROM `"+tableName+"` LEFT JOIN `"+tableNameR+"` ON `"+tableName+"`.`aid` = `"+tableNameR+"`.`bid` WHERE `"+tableName+"`.`aid` = "+aid+" AND `"+tableName+"`.`_t` = "+type+" AND `"+tableNameR+"`.`bid` = "+aid+" AND `"+tableNameR+"`.`_t` = "+type+" LEFT JOIN `"+tableNameB+"` ON `"+tableName+"`.`bid` = `"+tableNameB+"`.`_id` AND `"+tableName+"`.`_deleted` = 0");
//			return "SELECT DISTINCT `"+tableName+"`.`bid` FROM `"+tableName+"` LEFT JOIN `"+tableNameR+"` ON `"+tableName+"`.`aid` = `"+tableNameR+"`.`bid` LEFT JOIN `"+tableNameB+"` ON `"+tableName+"`.`bid` = `"+tableNameB+"`.`_id` WHERE `"+tableName+"`.`aid` = "+aid+" AND `"+tableName+"`.`_t` = "+type+" AND `"+tableNameR+"`.`bid` = "+aid+" AND `"+tableNameR+"`.`_t` = "+type+" AND `"+tableNameB+"`.`_deleted` = 0";
//		}
//		else
//		{
//			return "SELECT DISTINCT `"+tableName+"`.`bid` FROM `"+tableName+"` LEFT JOIN `"+tableNameR+"` ON `"+tableName+"`.`aid` = `"+tableNameR+"`.`bid` WHERE `"+tableName+"`.`aid` = "+aid+" AND `"+tableName+"`.`_t` = "+type+" AND `"+tableNameR+"`.`bid` = "+aid+" AND `"+tableNameR+"`.`_t` = "+type;
//		}
//	}
//	
	public static String getRelateValueSqlB(RelationClass a, RelationClass b, long bid, int type)
	{
		String tableName = BusinessMapDataStrategy.getRelationTableName(a, b);
		String tableNameR = BusinessMapDataStrategy.getRelationTableName(b, a);
		return "SELECT DISTINCT `"+tableName+"`.`aid` FROM `"+tableName+"` LEFT JOIN `"+tableNameR+"` ON `"+tableName+"`.`bid` = `"+tableNameR+"`.`aid` WHERE `"+tableName+"`.`bid` = "+bid+" AND `"+tableName+"`.`_t` = "+type+" AND `"+tableNameR+"`.`aid` = "+bid+" AND `"+tableNameR+"`.`_t` = "+type;
	}
//	
//	public static String getCheckRelateValueExistSql(RelationClass a, RelationClass b, long aid, long bid, int type)
//	{
//		String tableName = BusinessMapDataStrategy.getRelationTableName(a, b);
//		return "SELECT `_id` FROM `"+tableName+"` WHERE `aid` = "+aid+" AND `bid` = "+bid+" AND `_t` = "+type;
//	}
	
//	public static String getDeleteSql(RelationClass a, RelationClass b, long aid, int type)
//	{
//		String tableName = BusinessMapDataStrategy.getRelationTableName(a, b);
//		return "DELETE FROM `"+tableName+"` WHERE `aid` = "+aid+" AND `_t` = "+type;
//	}
//	
//	public static String getDeleteSql(RelationClass a, RelationClass b, long aid, long bid, int type)
//	{
//		String tableName = BusinessMapDataStrategy.getRelationTableName(a, b);
//		if(bid == 0)
//			return "DELETE FROM `"+tableName+"` WHERE `aid` = "+aid+" AND `_t` = "+type;
//		if(aid == 0)
//			return "DELETE FROM `"+tableName+"` WHERE `bid` = "+bid+" AND `_t` = "+type;
//		return "DELETE FROM `"+tableName+"` WHERE `aid` = "+aid+ " AND `bid` = " + bid + " AND `_t` = "+type;
//	}
}
